package top.mhtools.pigeon.wechat.service.support;

import com.google.common.collect.Maps;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import top.mhtools.pigeon.common.config.ApplicationContextHolder;

import java.util.Map;

/**
 * 企业微信消息发送执行策略
 *
 * @author machen
 * @date 2020/4/24 19:24
 */
@Component
public class WeChatMessageExecuteChooser implements ApplicationRunner {

    private Map<String, AbstractWeChatMessageExecuteStrategy> chooseMap = Maps.newHashMap();

    public AbstractWeChatMessageExecuteStrategy choose(String type) {
        return chooseMap.get(type);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        ApplicationContext applicationContext = ApplicationContextHolder.get();
        Map<String, AbstractWeChatMessageExecuteStrategy> solverMap = applicationContext.getBeansOfType(AbstractWeChatMessageExecuteStrategy.class);
        solverMap.values().forEach(each -> chooseMap.put(each.supports(), each));
    }
}
